﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileUploadASP
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FileUploadEntities fl = new FileUploadEntities();
            Image idb = (from iiiii in fl.Images
                         where iiiii.ID == 4
                         select iiiii).Single();
            img.ImageUrl = idb.FilePath;
            
            //img.ImageUrl = MapPath(idb.FilePath);
        }

        protected void Save(object sender, EventArgs e)
        {
            String savePath = MapPath("~/Styles/uploaded/");

            if (FileUpload1.HasFile)
            {
                String fileName = Guid.NewGuid() + FileUpload1.FileName;
                savePath += fileName;
                FileUpload1.SaveAs(savePath);
                updateProgress.Text = "Your file was saved as " + fileName;

                byte[] bytes = FileUpload1.FileBytes;
                FileUploadEntities fl = new FileUploadEntities();
                Image i = new Image();
                i.FilePath = "~/Styles/"+ Page.User.Identity.Name+"/uploaded/" + fileName;
                i.FileBytes = bytes;
                fl.Images.AddObject(i);
                fl.SaveChanges();

                


            }
            else
            {
                // Notify the user that a file was not uploaded.
                updateProgress.Text = "You did not specify a file to upload.";
            }
        }
    }
}
